from SysHelp import SysHelp as sh
from DrissionPage import ChromiumPage
from DrissionPage import SessionPage
from DrissionPage.easy_set import raise_when_ele_not_found
from DrissionPage.easy_set import set_paths
from DrissionPage.common import ActionChains
from datetime import datetime
import time
import re

sh.clear_screen()
raise_when_ele_not_found = False

s_playing = 'xpath://div[@class="vcp-player vcp-playing"]'
s_play_button = 'xpath://div[@class="vcp-playtoggle"]'
s_info_name = 'xpath://h2[@class="info-name"]'
s_end = 'xpath://div[@class="ended-mask"]'
s_next = 'xpath://div[@class="btns"]/p[1]'
s_continue = 'xpath://div[@class="alarmClock-wrapper"]'
s_score = 'xpath://div[@class="scoring-wrapper"]'
s_submit = 'xpath://div[@class="scoring-wrapper"]/div/div[@class="commit"]/button'
s_rate5 = 'xpath://div[@class="scoring-wrapper"]/div/div[@class="info-rate"]/div/span[5]'
s_learnd_status = 'xpath://div[@class="user-learn-status"]/img'
s_res_list = 'xpath://ul[@class="category-list"]'
s_media = 'xpath://i[@class="res-icon icon-media"]/following-sibling::p'


corses = []
  
# 读取所有链接
with open('link.txt', 'r') as file:  
    for line in file:
        if len(line.strip()) > 0:
            corses.append(line.strip())

# 循环打开页面
page = ChromiumPage()
ac = ActionChains(page)

for i in range(0, len(corses)):
    print(f"\n{datetime.now()}\t准备打开第 {i + 1} 个视频")
    if corses[i].startswith("#"):
        print(f"{datetime.now()}\t{corses[i][:50]}\t已被略过")
        continue
    else:
        page.get(corses[i])
        time.sleep(10)
    
    info_name = page.ele(s_info_name)
    if info_name:
        print(f"{datetime.now()}\t本节名称为:\t" + info_name.text.replace("\n", " "))

    #查找媒体
    media_list = []
    current_media = 0
    e_medias = page.eles(s_media)
    if e_medias and len(e_medias) >= 1:
        print(f"{datetime.now()}\t本节有 {len(e_medias)} 个媒体.")
        for i, media_ele in enumerate(e_medias, 0) :
            media_list.append(media_ele)
            if media_ele.parent(2).style("background-color")=="rgb(50, 136, 255)":
                print("\t\t\t\t\t" + media_ele.text + "   正在播放")
                current_media = i
            else:
                print("\t\t\t\t\t" + media_ele.text)
    else:
        print(f'{datetime.now()}\t没有找到一个视频,进入下一个页面.')
        continue

    while(1):
        # 没点播放?
        is_playing = page.s_ele(s_playing)
        if not is_playing:
            e_play_button = page.ele(s_play_button)
            # 有播放按钮
            if e_play_button:
                e_play_button.click()
                print(f"{datetime.now()}\t点击播放")
            # 没有播放按钮,可能是文档,点一个视频
            else:
                if media_list[current_media]:
                    media_list[current_media].click()
                    print(f"{datetime.now()}\t点击了  {media_list[current_media].text}")
                
        # 播放完毕?
        e_end = page.ele(s_end)
        if e_end:
            end_style = e_end.style('display')
        # 一节结束了
        if end_style != 'none':
            current_media += 1
            if current_media < len(media_list):
                if media_list[current_media]:
                    media_list[current_media].click()
                    print(f"{datetime.now()}\t点击了 {media_list[current_media].text}")
            else:
                print(f'{datetime.now()}\t好像全放完了,去往下一个页面')
                break
                            
        # 继续计时?
        e_alarm = page.ele(s_continue)
        if e_alarm:
            if e_alarm.style("display") != "none":
                e_alarm.click()
                print(f"{datetime.now()}\t点击了 继续计时")

        # 要给评价?
        e_score = page.ele(s_score)
        if e_score:
            is_score_displayed = e_score.style('display') != 'none'
            if is_score_displayed:
                e_rate5 = page.ele(s_rate5)
                if e_rate5:
                    ac.move_to(e_rate5)
                    ac.click()
                    # e_rate5.click()
                    print(f"{datetime.now()}\t点击了 评分5")
                e_submit = page.ele(s_submit)
                if e_submit:
                    e_submit.click()
                    print(f"{datetime.now()}\t点击了 评分提交")
                    
        # 学习时长已够?
        e_status = page.s_ele(s_learnd_status)
        if e_status:
            print(f"{datetime.now()}\t时长已够,开始下一个视频")
            break
        
        time.sleep(10)



